// components/wrtreehole/wrtreehole.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
   
    min:1,//最少字数
    max: 520, //最多字数 (根据自己需求改变) 
    tempImgFilePaths:[],
    msgId:'',
    sid:'',
    textValue:''

  },

  ready:function(){
    this.getMsgId.bind(this)();
    this.setData({
      sid:app.globalData.sid
    })
  }
,
  /**
   * 组件的方法列表
   */
  methods: {
    inputs: function (e) {
      // 获取输入框的内容
      var value = e.detail.value;
      // 获取输入框内容的长度
      var len = parseInt(value.length);
   
      //最少字数限制
      if(len <= this.data.min) 
          this.setData({
            texts: ""
          })
      else if(len > this.data.min)
      this.setData({
        texts: " "
      })
   
      //最多字数限制
      if(len > this.data.max) return;
          // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
          this.setData({
             currentWordNumber: len //当前字数  
          });
    },
  
    uploadimg:function(){
      wx.chooseImage({
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (result) => {
        
          var filepathArray=result.tempFilePaths;
           console.log(result.tempFilePaths);
          this.setData({
            tempImgFilePaths:result.tempFilePaths
          })

      



          for (var k=0,len=filepathArray.length;k<len;k++){
            var filepath=filepathArray[k]
            console.log("开始上传图片")
       
            wx.uploadFile({
              url: 'https://media.mogu.com/image/scale?appKey=15m&w=500&h=500&quality=100',
              filePath:filepath ,
              name:'image',
              header: {
                "content-type": "application/json"
              },
              formData: {
                method: 'POST' 
              },
              success: (result) => {
                console.log(result)
                var resData = result.data.replace(" ", "")
                resData = resData.replace(/\ufeff/g, "");
                var tempRes = JSON.parse(resData)
                var imgUrl=tempRes.result.surl;
                console.log(imgUrl)
                var msgId=this.data.msgId
                var sid=this.data.sid

                wx.request({
                  url: 'https://smart.songblog.top/addTreeHoleImg',
                  data: {msgId,sid,imgUrl},
                  header: {'content-type':'application/json'},
                  method: 'GET',
                  dataType: 'json',
                  responseType: 'text',
                  success: (result) => {
                    console.log(result.data)
                  },
                  fail: () => {},
                  complete: () => {}
                });
                  
  
  
  
  
              },
              fail: () => {},
              complete: () => {}
            });

          }

         
            
        },
        fail: () => {},
        complete: () => {}
      });
        
    },
    getMsgId:function(){

      var sid=app.globalData.sid
      wx.request({
        url: 'https://smart.songblog.top/queryMsgIdBySid',
        data: {sid},
        header: {'content-type':'application/json'},
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: (result) => {
          console.log(result.data);
          this.setData({
            msgId:result.data
          })
        },
        fail: () => {},
        complete: () => {}
      });
        
    },
    //发表按钮的点击事件，点击发表将文本内容提交到数据库

    addMsgText:function(e){
        var msgId=this.data.msgId
        var sid=this.data.sid
        // console.log(e)
        var msgText=this.data.textValue

      wx.request({
        url: 'https://smart.songblog.top/addTreeHoleMsg',
        data: {msgId,sid,msgText},
        header: {'content-type':'application/json'},
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: (result) => {
          console.log("插入文本成功")
          wx.showToast({
            title: '发表成功',
            icon: 'none',
            image: '../../images/send.png',
            duration: 1500,
            mask: false,
            success: (result) => {
              
            },
            fail: () => {},
            complete: () => {}
          });
            
        },
        fail: () => {},
        complete: () => {}
      });
        


    },

    getTextvalue:function(e){
      this.setData({
        textValue:e.detail.value
      })



    }



  },
 
})
